//
//  ActionDialog.swift
//  xzclient
//
//  Created by zhuxietong on 2021/5/4.
//

import Foundation
import soapp
import Eelay
import mosdks
 


public enum _DialogClick {
    case wx_friend
    case wx_pengyouquan
    case copy_link
    
    public static var defaultClicks:[_DialogClick] = [.wx_friend,.wx_pengyouquan]
    public static var tools:[_DialogClick] = [.copy_link]

    
    public var action:DialogAction{
        switch self {
        case .copy_link:
            return DialogAction(name: "复制链接", src: UIImage(share_named: "mosdk-pan-link"), click: _ModelClick({ (model,alert) in
                MoToast.share.do(mode: .info("已经复制内容连接".loc))
                UIPasteboard.general.string = MoShare.default.shareInfo["link",""]
            }))
        case .wx_friend:
            return DialogAction(name: "微信好友", src: UIImage(share_named: "mosdk-share-wx"), click: _ModelClick({ (model,alert) in
                SharePlatform.wx_friend.share(from: MoShare.default)
            }))
        case .wx_pengyouquan:
            return DialogAction(name: "朋友圈", src: UIImage(share_named: "mosdk-share-pyq"), click: _ModelClick({ (model,alert) in
                SharePlatform.wx_pengyquan.share(from: MoShare.default)
            }))
        }
    }
}
public class _ModelClick{
    public var block:(_:NSMutableDictionary,_:MoAlertController?)->Void
    public init(_ action:@escaping(_:NSMutableDictionary,_:MoAlertController?)->Void) {
        self.block = action
    }
}
public class DialogAction{
    public var click:_ModelClick? = nil
    public var name:String = ""
    public var icon:IMGSrc? = nil
    public init(name:String,src:IMGSrc?,click:_ModelClick?=nil) {
        self.name = name
        self.icon = src
        self.click = click
    }
}


public class ActionDialog<Body:ActionDialogBody>:MoFullBodyDialog<Body>{

    
    public override func viewDidLoad() {
        type = .sheet
        super.viewDidLoad()
        enabelBackTouch = true
    }
        
    
    
    public func qute(qute:ShareQute)->Self{
        MoShare().qute(qute)
        return self
    }
    
        
    
    
    public func clicked(action:@escaping (DialogAction)->Void)->Self{
        self.node._click = action
        return self
    }
    
    public func row1(clicks:[_DialogClick]) -> Self {
        node.row1 = clicks.map({ (k) -> DialogAction in
            return k.action
        })
        return self
    }
    public func row2(clicks:[_DialogClick]) -> Self {
        node.row2 = clicks.map({ (k) -> DialogAction in
            return k.action
        })
        return self
    }
    
    public func row1(actions:[DialogAction]) -> Self {
        node.row1 = actions
        return self
    }
    public func row2(actions:[DialogAction]) -> Self {
        node.row2 = actions
        return self
    }

    
    
}


open class ActionDialogKit:ASHorizontalKit{
    public let iconV = JoImageNode()
    public let nameL = ASTextNode2()
    public let bt = ASButtonNode()
    
    public override class func initCollectionNode() -> (ASCollectionNode, UICollectionViewFlowLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        let collectionNode = ASCollectionNode(collectionViewLayout: flowLayout)
        flowLayout.sectionInset = UIEdgeInsets(top: 10.fs, left: 20.fs, bottom: 10.fs, right: 20.fs)
        let h = 38.fs + 24.fs
        flowLayout.itemSize = [55.fs,h]
        flowLayout.minimumLineSpacing = 20.fs
        flowLayout.minimumInteritemSpacing = 20.fs
        flowLayout.scrollDirection = .horizontal
        collectionNode.showsHorizontalScrollIndicator = false
        collectionNode.showsVerticalScrollIndicator = false
        collectionNode.alwaysBounceHorizontal = true
        return (collectionNode,flowLayout)
    }
    
    open override class var collectionNodeSize: CGSize{
        get{
            let h = 38.fs + 20.fs + 24.fs * 2
            return [Swidth,h]
        }
    }
    
    open override func addLayoutRules() {
        bt.addTarget(self, action: #selector(clickAction), forControlEvents: .touchUpInside)
    }
    @objc open func clickAction(){
        active(ID:"click", object: mo.model)
        if let raw = mo.model["raw"] as? DialogAction{
            raw.click?.block(mo.model,mo.controller as? MoAlertController)
        }
    }
 
    
    open override func mo_model_reload() {
        iconV.src = mo.model["icon"]
        nameL.attributedText = mo.model["name",""].ui.center.font12.color(color: ._second).astring
    }
    
    var hasName:Bool{
        return mo.model["name",""].len > 0
    }
    
    open override func specGetter() -> _Spec {
        return _Spec.stack(["v|\(8.fs)",&-.center], childs: [
            iconV["\(38.fs)",38.fs],
            .if(hasName, spec: nameL.Spec)
        ]).addOver(bt.Spec)
    }
}

public class ActionDialogBody:DialogContent, JoTableCellDelegate{
    public func touch(cell: Any, actionID: String, model: NSMutableDictionary) {
        if actionID == "click" {
            if let raw = model["raw"] as? DialogAction{
                _click(raw)
                alert?.hidden()
            }
        }
    }
    
    var _click:(_:DialogAction)->Void = {_ in}

    
    
    public var row1:[DialogAction] = _DialogClick.defaultClicks.map { (c) -> DialogAction in
        return c.action
    }{
        didSet{
            rowList1.mo.model = [
                "list":row1.map({ (action) -> [String:Any] in
                    return ["name":action.name,"icon":action.icon ?? "","raw":action]
                })
            ].mutable_dictionary
        }
    }
    public var row2:[DialogAction] = _DialogClick.tools.map { (c) -> DialogAction in
        return c.action
    }{
        didSet{
            rowList2.mo.model = [
                "list":row2.map({ (action) -> [String:Any] in
                    return ["name":action.name,"icon":action.icon ?? "","raw":action]
                })
            ].mutable_dictionary
        }
    }
    
    let titleL = ASTextNode2()

    let line1 = ASDisplayNode()
    let line2 = ASDisplayNode()
    let cancelBt = ASButtonNode()
    let rowList1:ASHorizontalListNode<ActionDialogKit> = ASHorizontalListNode<ActionDialogKit>()
    let rowList2:ASHorizontalListNode<ActionDialogKit> = ASHorizontalListNode<ActionDialogKit>()

    public override func addLayoutRules() {
        titleL.attributedText = "分享".ui.mfont16.color(color: ._foreground).lineSpace(5).astring
      
        rowList1.delegate = self
        rowList2.delegate = self
        rowList1.mo.model = [
            "list":row1.map({ (action) -> [String:Any] in
                return ["name":action.name,"icon":action.icon ?? "","raw":action]
            })
        ].mutable_dictionary
        
        rowList2.mo.model = [
            "list":row2.map({ (action) -> [String:Any] in
                return ["name":action.name,"icon":action.icon ?? "","raw":action]
            })
        ].mutable_dictionary
        line1.backgroundColor = ._separator
        line2.backgroundColor = ._separator
        cancelBt.ui.ntitle(title: "取消").mfont(size: 14).ncolor(color: ._second).padding(insets: [10,30]).touchUpInside {[weak self] (bt) in
            self?.alert?.hidden()
        }
    }
    

    public override func mo_model_reload() {
        
    }
    
    public override func specGetter() -> _Spec {

      
        return .stack(["v",&-.stretch], childs: [
            .inset([], [18,24], child: titleL.Spec),
            .if(row1.count > 0, spec: rowList1.Spec),
            .if(row1.count > 0, spec: line1["\(TP.separatorHeight)"]),
            .if(row2.count > 0, spec: rowList2.Spec),
            .if(row2.count > 0, spec: line2["\(TP.separatorHeight)"]),
            .stack(["8"], childs: []),
            cancelBt.Spec,
            .stack(["8"], childs: [])
        ])
    }
}


